package com.example.project.mapper;

import java.util.List;
import org.apache.ibatis.annotations.*;
import com.example.project.entity.JournalDetail;

/**
 * 变更详情，存储journal记录的详细变更信息Mapper接口
 * 
 * @author ruoyi
 * @date 2025-09-26
 */
@Mapper
public interface JournalDetailMapper {
	
	/**
	 * 查询变更详情，存储journal记录的详细变更信息
	 * 
	 * @param id 变更详情，存储journal记录的详细变更信息主键
	 * @return 变更详情，存储journal记录的详细变更信息
	 */
	@Select("select id, journal_id, property, prop_key, description, old_value, value from t_journal_detail where id = #{id}")
	public JournalDetail selectJournalDetailsById(Long id);

	/**
	 * 查询变更详情，存储journal记录的详细变更信息列表
	 * 
	 * @param tJournalDetails 变更详情，存储journal记录的详细变更信息
	 * @return 变更详情，存储journal记录的详细变更信息集合
	 */
	@Select("<script>" +
			"select id, journal_id, property, prop_key, description, old_value, value from t_journal_detail" +
			"<where>" +
			"<if test='journalId != null'> and journal_id = #{journalId}</if>" +
			"<if test='property != null and property != \"\"'> and property = #{property}</if>" +
			"<if test='propKey != null and propKey != \"\"'> and prop_key = #{propKey}</if>" +
			"<if test='oldValue != null and oldValue != \"\"'> and old_value = #{oldValue}</if>" +
			"<if test='value != null and value != \"\"'> and value = #{value}</if>" +
			"</where>" +
			"</script>")
	public List<JournalDetail> selectJournalDetailsList(JournalDetail tJournalDetails);

	/**
	 * 新增变更详情，存储journal记录的详细变更信息
	 * 
	 * @param tJournalDetails 变更详情，存储journal记录的详细变更信息
	 * @return 结果
	 */
	@Insert("<script>" +
			"insert into t_journal_detail" +
			"<trim prefix='(' suffix=')' suffixOverrides=','>" +
			"<if test='journalId != null'>journal_id,</if>" +
			"<if test='property != null and property != \"\"'>property,</if>" +
			"<if test='propKey != null and propKey != \"\"'>prop_key,</if>" +
			"<if test='description != null and description != \"\"'>description,</if>" +
			"<if test='oldValue != null'>old_value,</if>" +
			"<if test='value != null'>value,</if>" +
			"</trim>" +
			"<trim prefix='values (' suffix=')' suffixOverrides=','>" +
			"<if test='journalId != null'>#{journalId},</if>" +
			"<if test='property != null and property != \"\"'>#{property},</if>" +
			"<if test='propKey != null and propKey != \"\"'>#{propKey},</if>" +
			"<if test='description != null and description != \"\"'>#{description},</if>" +
			"<if test='oldValue != null'>#{oldValue},</if>" +
			"<if test='value != null'>#{value},</if>" +
			"</trim>" +
			"</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertJournalDetails(JournalDetail tJournalDetails);

	/**
	 * 修改变更详情，存储journal记录的详细变更信息
	 * 
	 * @param tJournalDetails 变更详情，存储journal记录的详细变更信息
	 * @return 结果
	 */
	@Update("<script>" +
			"update t_journal_detail" +
			"<trim prefix='SET' suffixOverrides=','>" +
			"<if test='journalId != null'>journal_id = #{journalId},</if>" +
			"<if test='property != null and property != \"\"'>property = #{property},</if>" +
			"<if test='propKey != null and propKey != \"\"'>prop_key = #{propKey},</if>" +
			"<if test='description != null and description != \"\"'>description = #{description},</if>" +
			"<if test='oldValue != null'>old_value = #{oldValue},</if>" +
			"<if test='value != null'>value = #{value},</if>" +
			"</trim>" +
			"where id = #{id}" +
			"</script>")
	public int updateJournalDetails(JournalDetail tJournalDetails);

	/**
	 * 删除变更详情，存储journal记录的详细变更信息
	 * 
	 * @param id 变更详情，存储journal记录的详细变更信息主键
	 * @return 结果
	 */
	@Delete("delete from t_journal_detail where id = #{id}")
	public int deleteJournalDetailsById(Long id);

	/**
	 * 批量删除变更详情，存储journal记录的详细变更信息
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
	@Delete("<script>" +
			"delete from t_journal_detail where id in " +
			"<foreach item='id' collection='array' open='(' separator=',' close=')'>" +
			"#{id}" +
			"</foreach>" +
			"</script>")
	public int deleteJournalDetailsByIds(String[] ids);

	@Select("select id, journal_id, property, prop_key, description, old_value, value from t_journal_detail where journal_id = #{journalId}")
	public List<JournalDetail> selectListByJournalId(long journalId);

	@Select("select d.id, d.journal_id, d.property, d.prop_key, d.description, d.old_value, d.value, j.created_on as createTime "
			+ "from t_journal_detail d, t_journal j where d.journal_id  = j.id "
			+ "and j.journalized_id = #{issueId} and j.journalized_type = #{journalizedType} "
			+ "and d.prop_key = #{propKey} and d.value != #{authorId} order by j.created_on desc limit 1")
	public JournalDetail queryByIssueIdAndPropKey(Long issueId,String journalizedType, String propKey, Long authorId);
}
